package com.example.myapplication.fragment

import android.annotation.SuppressLint
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import com.example.myapplication.MainViewModel
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentUserFirstLoginBinding
import com.example.myapplication.db.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.coroutines.cancellation.CancellationException

class UserFirstLoginFragment : BaseFragment<FragmentUserFirstLoginBinding>() {
    private val mViewModel: MainViewModel by activityViewModels()

    override fun initBinding(): FragmentUserFirstLoginBinding {
        return FragmentUserFirstLoginBinding.inflate(layoutInflater)
    }

    override fun initView() {
        var thisAccount:String = ""
        mViewModel.loggedInUser.observe(viewLifecycleOwner, Observer { loggedInUser ->
            if (loggedInUser != "") {
                thisAccount=loggedInUser
            }
        })
        mBinding.editTextBio.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                // 在文本改变之前调用
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                // 在文本改变时调用
                val length = s?.length ?: 0 // 获取当前输入的字数
                mBinding.charCount.text=length.toString()+"/300"
            }

            override fun afterTextChanged(s: Editable?) {
                // 在文本改变之后调用
            }
        })
        mBinding.profileImage.setOnClickListener {

        }
        mBinding.saveButton.setOnClickListener {

//            if(mBinding.profileImage.background.constantState== context?.let { it1 ->
//                    ContextCompat.getDrawable(
//                        it1, R.drawable.user)?.constantState
//                }){
//                Toast.makeText(context,"不妨先来个自己最喜欢的头像！",Toast.LENGTH_SHORT).show()
//            }else
                if(mBinding.editTextBio.text.toString()==""){
                Toast.makeText(context, "试着输入个人简介吧！", Toast.LENGTH_SHORT).show()
            }else {

                    lifecycleScope.launch(Dispatchers.IO) {
                        val user = mViewModel.findUserByAccount(thisAccount)

                        user?.let {
                            try {
                                mViewModel.fillUserInformation(it.id, mBinding.editTextBio.text.toString())
                            } catch (e: CancellationException) {
                                // 处理协程取消的情况
                                return@launch
                            }
                        }

                        withContext(Dispatchers.Main) {
                            findNavController().navigate(R.id.action_userFirstLoginFragment_to_homeFragment)
                        }
                    }
                }
        }

    }


}